package com.kejiguan.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.kejiguan.ui.R;
import com.kejiguan.ui.adapter.SheduleObjectAdapter;
import com.kejiguan.ui.adapter.SheduleOperationAdapter;
import com.kejiguan.ui.adapter.SheduleTimeTypeAdapter;
import com.kejiguan.ui.adapter.SheduleWeekAdapter;
import com.kejiguan.ui.adapter.TimeSpinnerAdapter;
import com.kejiguan.ui.model.DisplayArea;
import com.kejiguan.ui.model.ExhibitionNeItem;
import com.kejiguan.ui.model.Shedule;
import com.kejiguan.ui.model.SheduleActionType;
import com.kejiguan.ui.model.SheduleTypeModel;
import com.kejiguan.ui.model.WeekItem;
import com.kejiguan.ui.util.CacheDataUtil;
import com.kejiguan.ui.util.CommonDate;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by apple on 16/5/12.
 */
public class AddSheduleDialog extends Dialog implements View.OnClickListener {

    private View mDialogView;
    private EditText sheduleNameInput;
    private Spinner sheduleTypeSpinner;
    private Spinner hourSpinner, minSpinner, secSpinner;
    private Spinner sheduleActionSpinner;
    private Spinner sheduleObjectSpinner;
    private Spinner weekSpinner;

    private View addBtn, cancelBtn;
    private AddSheduleListerner addSheduleListerner;

    public AddSheduleDialog(Context context) {
        super(context);
        init(context);
    }

    public AddSheduleDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected AddSheduleDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
        setCanceledOnTouchOutside(true);
    }

    private void init(Context context) {

        mDialogView = View.inflate(context, R.layout.dialog_add_shedule_layout, null);

        sheduleNameInput = (EditText) mDialogView.findViewById(R.id.shedule_name_input);
        sheduleTypeSpinner = (Spinner) mDialogView.findViewById(R.id.type_select);
        weekSpinner = (Spinner) mDialogView.findViewById(R.id.week_select);

        hourSpinner = (Spinner) mDialogView.findViewById(R.id.hour_select);
        minSpinner = (Spinner) mDialogView.findViewById(R.id.min_select);
        secSpinner = (Spinner) mDialogView.findViewById(R.id.sec_select);

        sheduleActionSpinner = (Spinner) mDialogView.findViewById(R.id.action_select);
        sheduleObjectSpinner = (Spinner) mDialogView.findViewById(R.id.object_select);

        addBtn = mDialogView.findViewById(R.id.add_shedule_confirm_btn);
        cancelBtn = mDialogView.findViewById(R.id.add_shedule_cancel_btn);
        addBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);

        hourSpinner.setAdapter(new TimeSpinnerAdapter(context, 0, 23));
        minSpinner.setAdapter(new TimeSpinnerAdapter(context, 0, 59));
        secSpinner.setAdapter(new TimeSpinnerAdapter(context, 0, 59));
        sheduleActionSpinner.setAdapter(new SheduleOperationAdapter(getContext(), CommonDate.actionTypes));
        weekSpinner.setAdapter(new SheduleWeekAdapter(getContext(), CommonDate.weekItems));
        sheduleTypeSpinner.setAdapter(new SheduleTimeTypeAdapter(getContext(), CommonDate.sheduleTypeModels));
        sheduleTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Log.e(AddSheduleDialog.class.getSimpleName(), "position:" + position);
                if (position == (sheduleTypeSpinner.getAdapter().getCount() - 1)) {
                    weekSpinner.setVisibility(View.VISIBLE);
                } else {
                    weekSpinner.setVisibility(View.GONE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        List<ExhibitionNeItem> exhibitionNeItems = new ArrayList<>();
        ExhibitionNeItem exhibitionNeItem = new ExhibitionNeItem();
        exhibitionNeItem.setExhibitionId("TALL");
        exhibitionNeItem.setExhibitionName("所有");
        exhibitionNeItems.add(exhibitionNeItem);
        List<DisplayArea> displayAreas = CacheDataUtil.getInstance().getDisplayAreas();


        for (int i = 0; i < displayAreas.size(); i++) {

            DisplayArea displayArea = displayAreas.get(i);
            exhibitionNeItems.add(new ExhibitionNeItem(displayArea.getDisplayId(),displayArea.getDisplayId()+displayArea.getDisplayName()));
            exhibitionNeItems.addAll(displayArea.getExhibitionNeItems());

        }

        sheduleObjectSpinner.setAdapter(new SheduleObjectAdapter(getContext(), exhibitionNeItems));
        setContentView(mDialogView);
        setCanceledOnTouchOutside(true);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.add_shedule_cancel_btn:
                dismiss();
                break;
            case R.id.add_shedule_confirm_btn:

                if (null != addSheduleListerner) {
                    String sheduleName = sheduleNameInput.getText().toString();
                    if (TextUtils.isEmpty(sheduleName)) {
                        Toast.makeText(getContext(), "任务名不能为空", Toast.LENGTH_SHORT).show();
                        break;
                    }

                    SheduleTimeTypeAdapter sheduleTimeTypeAdapter = (SheduleTimeTypeAdapter) sheduleTypeSpinner.getAdapter();
                    String timeType = ((SheduleTypeModel) sheduleTimeTypeAdapter.getItem(sheduleTypeSpinner.getSelectedItemPosition())).getTypeId();
                    if ("5".equals(timeType)) {
                        SheduleWeekAdapter weekAdapter = (SheduleWeekAdapter) weekSpinner.getAdapter();
                        String weekType = ((WeekItem) weekAdapter.getItem(weekSpinner.getSelectedItemPosition())).getWeekId();
                        timeType = timeType + "," + weekType;
                    }

                    SheduleOperationAdapter operationAdapter = (SheduleOperationAdapter) sheduleActionSpinner.getAdapter();
                    String actionType = ((SheduleActionType) operationAdapter.getItem(sheduleActionSpinner.getSelectedItemPosition())).getActionId();

                    SheduleObjectAdapter sheduleObjectAdapter = (SheduleObjectAdapter) sheduleObjectSpinner.getAdapter();
                    String actionObjerct = ((ExhibitionNeItem) sheduleObjectAdapter.getItem(sheduleObjectSpinner.getSelectedItemPosition())).getExhibitionId();

                    int hour = hourSpinner.getSelectedItemPosition();
                    int min = minSpinner.getSelectedItemPosition();
                    int sec = secSpinner.getSelectedItemPosition();

                    String hourS = hour < 10 ? "0" + hour : String.valueOf(hour);
                    String minS = min < 10 ? "0" + min : String.valueOf(min);
                    String secS = sec < 10 ? "0" + sec : String.valueOf(sec);

                    Shedule shedule = new Shedule();
                    shedule.setScheduleName(sheduleName);
                    shedule.setScheduleType(timeType);
                    shedule.setScheduleAction(actionType);
                    shedule.setScheduleObject(actionObjerct);
                    shedule.setScheduleNote("");
                    shedule.setScheduleTime(hourS + ":" + minS + ":" + secS);
                    shedule.setScheduleState("0");
                    addSheduleListerner.addShedule(shedule);
                }
                dismiss();
                break;
        }
    }

    public void setAddSheduleListerner(AddSheduleListerner addSheduleListerner) {
        this.addSheduleListerner = addSheduleListerner;
    }

    public interface AddSheduleListerner {
        public void addShedule(Shedule shedule);
    }
}
